<?php
/**
 * Api Controller class file
 *  
 * @package Jurnalport
 */

/**
 * Api Controller class
 * 
 * @uses Apricot_Controller
 * @package Jurnalport
 * @author Jansen Price <jansen.price@gmail.com>
 * @version $Id$
 */
class ApiController extends Apricot_Controller
{
    /**
     * Initialize object
     * 
     * @return void
     */
    public function init()
    {
        include_once 'Apricot/View/Simple.php';
        $viewOptions = array(
            'engine' => 'Apricot_View_Simple',
        );

        $this->setView($this->_kernel->makeView($viewOptions));

        $this->_view->shouldRenderAction = false;
        $this->_view->useLayout          = false;

        $this->_response = array(
            'status' => true,
            'result' => '',
        );
    }

    /**
     * Index action
     * 
     * @return void
     */
    public function action__index()
    {
        $this->_response['result'] = 'ready';
        $this->vbuf(json_encode($this->_response));
    }

    /**
     * Retrieve entries
     * 
     * @return void
     */
    public function action__retrieve()
    {
        $journal = $this->_kernel->getModel('journal');

        $limit = $this->_request->limit;

        $entries = $journal->retrieve($limit);

        $results = array();
        if ($entries) {
            foreach ($entries as $entry) {
                $results[] = array(
                    'id'   => $entry->id,
                    'date' => $entry->entry_date,
                    'text' => $entry->entry_text,
                );
            }
            $results = array_reverse($results);
        }

        $this->_response['result'] = $results;
        $encryptedResponse = $this->_kernel->getModule('encryption')
            ->encrypt(json_encode($this->_response));
        $this->vbuf($encryptedResponse);
    }

    /**
     * Retrieve just the headings
     * 
     * @return void
     */
    public function action__retrieveHeadings()
    {
        $journal = $this->_kernel->getModel('journal');

        $limit = $this->_request->limit;

        $entries = $journal->retrieve($limit);

        $results = array();
        if ($entries) {
            foreach ($entries as $entry) {
                $results[] = array(
                    'id'   => $entry->id,
                    'date' => $entry->entry_date,
                    'text' => substr($entry->entry_text, 0, 64)
                );
            }
            $results = array_reverse($results);
        }

        $this->_response['result'] = $results;
        $encryptedResponse = $this->_kernel->getModule('encryption')
            ->encrypt(json_encode($this->_response));
        $this->vbuf($encryptedResponse);
    }

    /**
     * Add an entry
     * 
     * @return void
     */
    public function action__add()
    {
        if ($this->_request->isPost()) {
            $post    = $this->_request->getPost();
            $date    = $post->date;
            $message = $post->message;
        } else {
            $date    = $this->_request->date;
            $message = $this->_request->message;
        }

        if (!$date) {
            $date = null;
        }

        $journal = $this->_kernel->getModel('journal');
        $journal->save($message, $date);

        $this->_response['result'] = 'Saved entry';

        $encryptedResponse = $this->_kernel->getModule('encryption')
            ->encrypt(json_encode($this->_response));
        $this->vbuf($encryptedResponse);
    }
}
